package com.touchalachs.game;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.MotionEvent;

import com.touchalachs.core.StringTool;
import com.touchalachs.core.ui.BasicUIElement;
import com.touchalachs.core.ui.Button;
import com.touchalachs.core.ui.ButtonListener;
import com.touchalachs.core.ui.Label;
import com.touchalachs.core.ui.TextArea;
import com.touchalachs.core.ui.TippableBasicUIElement;

public class WorkerInfoPopup extends BasicUIElement implements ButtonListener{
	
	private final Calculator calc;
	private final Label titleLabel;
	private final TextArea descriptionTextArea;
	private final Label countLabel;
	private final Label priceLabel;
	private final Button buyButton;
	private volatile boolean visible;
	private volatile int offeringIndex;

	public WorkerInfoPopup(Point screenSize, Calculator calc) {
		super(0, 50, 100, 50, screenSize);
		this.calc = calc;
		titleLabel = new Label(5, 51, 90, 8, "", screenSize);
		titleLabel.setTextSize(40);
		descriptionTextArea = new TextArea(5, 59, 90, 10, "", screenSize);
		descriptionTextArea.setTextSize(24);
		countLabel = new Label(5, 78, 90, 5, "", screenSize);
		countLabel.setTextSize(24);
		priceLabel = new Label(5, 86, 90, 5, "", screenSize);
		priceLabel.setTextSize(24);
		buyButton = new Button(5, 91, 40, 5, Res.BUY, screenSize, this);
		visible = false;
	}

	public void showPopup(String title, String descripton, int index, int alreadyOwned, int cost){
		titleLabel.setText(title);
		descriptionTextArea.setText(descripton);
		countLabel.setText(Res.ALREADY_OWNED+": "+alreadyOwned);
		priceLabel.setText(Res.PRICE+": "+StringTool.format(cost)+" "+Res.LACHSE);
		offeringIndex = index;
		visible = true;
	}
	
	@Override
	public void draw(Canvas canvas) {
		if (visible){
			Paint p = new Paint();
			p.setARGB(127, 0, 0, 0);
			canvas.drawPaint(p);
			p.setARGB(255, 32, 32, 32);
			canvas.drawRect(getDimension(), p);
			titleLabel.draw(canvas);
			descriptionTextArea.draw(canvas);
			countLabel.draw(canvas);
			priceLabel.draw(canvas);
			buyButton.draw(canvas);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (visible) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				buyButton.onTouchEvent(event);
				visible = false;
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean onTipp(TippableBasicUIElement source) {
		calc.buyWorker(offeringIndex);
		return true;
	}
}
